package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerProblemBackListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerProblemBack;
import com.isunland.managesystem.entity.CustomerProblemBackOriginal;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Lenovo on 2017/3/1.
 */
public class CustomerProblemBackListFragment extends BaseListFragment {

  private ArrayList<CustomerProblemBack> mList;
  private final int REQUEST_DETAIL = 0;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionBar = ((BaseVolleyActivity) getActivity()).getSupportActionBar();
    if (actionBar != null) {
      actionBar.setTitle(R.string.problem);
    }
    mList = new ArrayList<>();
  }

  @Override public String getUrl() {
    return ApiConst.URL_PROBLEM_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("regStaffId", CurrentUser.newInstance(getActivity()).getJobNumber());
    return params;
  }

  @Override public void resolveResponse(String json) {
    CustomerProblemBackOriginal result =
        new Gson().fromJson(json, CustomerProblemBackOriginal.class);
    if (result.getResult() == Base.SUCCESS && result.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(result.getRows());
      CustomerProblemBackListAdapter mAdapter =
          new CustomerProblemBackListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_add:
        Intent intent = new Intent(getActivity(), AddCustomerProblemBackActivity.class);
        startActivityForResult(intent, REQUEST_DETAIL);

        return true;
      default:

        return super.onOptionsItemSelected(item);
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    CustomerProblemBack content = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), CustomerProblemBackDetailActivirty.class);
    intent.putExtra(CustomerProblemBackDetailFragment.EXTRA_CONTENT, content);
    startActivityForResult(intent, REQUEST_DETAIL);

    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }
}
